<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <base href="${APP_PATH}/">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <title>教学管理系统</title>
    <link rel="icon" href="public/favicon.ico" type="image/ico">
    <%@include file="/commons.jsp" %>
    <link rel="stylesheet" href="/public/css/zTreeStyle.css">
    <script src="/public/js/jquery.ztree.all-3.5.min.js"></script>
</head>

<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <%@ include file="/menu.jsp" %>
        <!--End 左侧导航-->
        <!--头部信息-->
        <%@ include file="/head.jsp" %>
        <!--End 头部信息-->
        <!--页面主要内容-->
        <main class="lyear-layout-content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-toolbar clearfix">
                                <div class="toolbar-btn-action">
                                    <a class="btn btn-primary m-r-5" onclick="assign()"><i class="mdi mdi-plus"></i>分配权限</a>
                                </div>
                            </div>
                            <div class="card-body">
                                <ul id="treeDemo" class="ztree"></ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>
<script type="text/javascript">
    let funcTreeObj;

    let setting = {
        async: {
            enable: true,
            url: "manage/permission/assignPermissionShow",
        },
        check: {
            enable: true
        },
        view: {
            expandSpeed: "normal",
            selectedMulti: false,
        },
    };

    function show() {
        funcTreeObj = $.fn.zTree.init($("#treeDemo"), setting);
        setTimeout(init, 50);
    }

    function assign() {
        let info = "";
        let checkedObjs = funcTreeObj.getCheckedNodes(true);  //获取所有选中的节点
        for (let i = 0; i < checkedObjs.length; i++) {
            info += checkedObjs[i].pid + ","
        }
        $.ajax({
            url: "manage/rolepermission/assignDo",
            data: {"rid":${rid}, "pids": info},
            type: "post",
            success: function (obj) {
                if (obj.result) {
                    layer.msg("分配权限成功", {time: 1000, icon: 6, shift: 6}, function () {
                        show();
                    })
                } else {
                    layer.msg("分配权限失败", {time: 1000, icon: 5, shift: 6}, function () {
                    })
                }
            }
        });
    }

    function init() {
        $.ajax({
            url: "manage/rolepermission/assignList",
            data: {"rid":${rid}},
            type: "post",
            success: function (obj) {
                if (obj.result) {
                    if (obj.pids != null) {
                        let datas = obj.pids.split(',');
                        for (let i = 0; i < datas.length; i++) {
                            let node = funcTreeObj.getNodeByParam("pid", datas[i]);
                            console.log(node);
                            if (node != null) {
                                funcTreeObj.checkNode(node, true, false);
                            }
                        }
                    }
                } else {
                    layer.msg("系统繁忙，稍后再试", {time: 1000, icon: 5, shift: 6}, function () {
                    })
                }
            }
        })
    }

    $(document).ready(function () {
        layer.msg("加载中", {time: 200, icon: 16, shift: 5}, function () {
            show();
        });//layer
    });//入口
</script>
</body>
</html>

